﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;

internal class PostProcessStandalonePlayer
{
    private static void CopyUsingRelativePath(string sourceRoot, string destinationRoot, string relativePath)
    {
        FileUtil.CopyFileOrDirectory(Path.Combine(sourceRoot, relativePath), Path.Combine(destinationRoot, relativePath));
    }

    private static string GetArchitectureForTarget(BuildTarget target)
    {
        BuildTarget target2 = target;
        switch (target2)
        {
            case BuildTarget.StandaloneLinux:
            case BuildTarget.MetroPlayerX86:
                break;

            case BuildTarget.StandaloneWindows64:
            case BuildTarget.MetroPlayerX64:
            case BuildTarget.StandaloneLinux64:
                return "x86_64";

            default:
                if ((target2 != BuildTarget.StandaloneOSXIntel) && (target2 != BuildTarget.StandaloneWindows))
                {
                    return string.Empty;
                }
                break;
        }
        return "x86";
    }

    public static void PostProcessStandaloneLinuxPlayer(BuildTarget target, BuildOptions options, string installPath, string stagingAreaData, string stagingArea, string playerPackage)
    {
        string[] textArray1 = new string[] { "LinuxPlayer", "Data/Resources", "Data/Mono" };
        foreach (string str in textArray1)
        {
            CopyUsingRelativePath(playerPackage, stagingArea, str);
        }
        string dir = Path.Combine(stagingArea, Path.Combine("Data/Mono", GetArchitectureForTarget(target)));
        FileUtil.CreateOrCleanDirectory(dir);
        FileUtil.MoveFileOrDirectory(Path.Combine(stagingArea, "Data/Mono/libmono.so"), Path.Combine(dir, "libmono.so"));
        PostprocessBuildPlayer.InstallPlugins(stagingArea + "/Data/Plugins", target);
        string str3 = FileUtil.UnityGetFileNameWithoutExtension(installPath);
        string str4 = FileUtil.UnityGetDirectoryName(installPath);
        string str5 = str3 + "_Data";
        string path = Path.Combine(str4, str5);
        FileUtil.DeleteFileOrDirectory(installPath);
        FileUtil.DeleteFileOrDirectory(path);
        FileUtil.MoveFileOrDirectory(stagingArea + "/Data", path);
        FileUtil.MoveFileOrDirectory(stagingArea + "/LinuxPlayer", installPath);
    }

    public static void PostProcessStandaloneOSXPlayer(BuildTarget target, BuildOptions options, string installPath, string stagingAreaData, string stagingArea, string playerPackage, string companyName, string productName)
    {
        string str = stagingArea + "/UnityPlayer.app/Contents";
        if (Directory.Exists("Assets/StreamingAssets"))
        {
            FileUtil.CopyDirectoryRecursiveForPostprocess("Assets/StreamingAssets", stagingAreaData + "/StreamingAssets", true);
        }
        if ((options & BuildOptions.InstallInBuildFolder) != BuildOptions.None)
        {
            PostprocessBuildPlayer.PostprocessStandalonePlayerInBuildFolder(stagingAreaData);
        }
        else
        {
            FileUtil.CopyDirectoryRecursive(Path.Combine(playerPackage, "UnityPlayer.app"), Path.Combine(stagingArea, "UnityPlayer.app"));
            string str2 = PostprocessBuildPlayer.GenerateBundleIdentifier(companyName, productName);
            string[] input = new string[] { "UNITY_BUNDLE_IDENTIFIER", str2, ">UnityPlayer<", ">" + productName + "<" };
            FileUtil.ReplaceText(str + "/Info.plist", input);
            RemoveArch(str + "/Frameworks/MonoEmbedRuntime/osx/libmono.0.dylib", target);
            RemoveArch(str + "/MacOS/UnityPlayer", target);
            File.Move(str + "/MacOS/UnityPlayer", str + "/MacOS/" + productName);
            FileUtil.MoveFileOrDirectory(stagingAreaData, str + "/Data");
            PostprocessBuildPlayer.InstallPlugins(str + "/Plugins", target);
            FileUtil.DeleteFileOrDirectory(installPath);
            FileUtil.MoveFileOrDirectory(stagingArea + "/UnityPlayer.app", installPath);
        }
    }

    public static void PostProcessStandaloneWindowsPlayer(BuildTarget target, BuildOptions options, string installPath, string stagingAreaData, string stagingArea, string playerPackage)
    {
        string str3;
        List<string> list = new List<string> { "player_win.exe", "Data/Mono" };
        if ((options & BuildOptions.Development) != BuildOptions.None)
        {
            list.Add("UnityPlayer_Symbols.pdb");
        }
        if (target == BuildTarget.StandaloneGLESEmu)
        {
            list.AddRange(new List<string> { "libEGL.dll", "libgles_cl.dll", "libgles_cm.dll", "libGLESv2.dll" });
        }
        foreach (string str in list)
        {
            FileUtil.CopyFileOrDirectory(playerPackage + "/" + str, stagingArea + "/" + str);
        }
        string path = stagingArea + "/Data/Resources";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        if (target == BuildTarget.StandaloneGLESEmu)
        {
            str3 = "ResourcesGLES/unity default resources";
        }
        else
        {
            str3 = "Resources/unity default resources";
        }
        FileUtil.CopyFileOrDirectory(playerPackage + "/Data/" + str3, stagingArea + "/Data/Resources/unity default resources");
        PostprocessBuildPlayer.InstallPlugins(stagingArea + "/Data/Plugins", target);
        if (Directory.Exists("Assets/StreamingAssets"))
        {
            FileUtil.CopyDirectoryRecursiveForPostprocess("Assets/StreamingAssets", stagingAreaData + "/StreamingAssets", true);
        }
        if ((options & BuildOptions.InstallInBuildFolder) != BuildOptions.None)
        {
            PostprocessBuildPlayer.PostprocessStandalonePlayerInBuildFolder(stagingAreaData);
        }
        else
        {
            string str4 = FileUtil.UnityGetFileNameWithoutExtension(installPath);
            string str5 = FileUtil.UnityGetDirectoryName(installPath);
            string str6 = str4 + "_Data";
            string str7 = Path.Combine(str5, str6);
            string str8 = Path.Combine(str5, "UnityPlayer_Symbols.pdb");
            FileUtil.DeleteFileOrDirectory(installPath);
            FileUtil.DeleteFileOrDirectory(str7);
            FileUtil.DeleteFileOrDirectory(str8);
            FileUtil.MoveFileOrDirectory(stagingArea + "/Data", str7);
            FileUtil.MoveFileOrDirectory(stagingArea + "/player_win.exe", installPath);
            if ((options & BuildOptions.Development) != BuildOptions.None)
            {
                FileUtil.MoveFileOrDirectory(stagingArea + "/UnityPlayer_Symbols.pdb", str8);
            }
            if (target == BuildTarget.StandaloneGLESEmu)
            {
                FileUtil.ReplaceFile(stagingArea + "/libEGL.dll", str5 + "/libEGL.dll");
                FileUtil.ReplaceFile(stagingArea + "/libgles_cl.dll", str5 + "/libgles_cl.dll");
                FileUtil.ReplaceFile(stagingArea + "/libgles_cm.dll", str5 + "/libgles_cm.dll");
                FileUtil.ReplaceFile(stagingArea + "/libGLESv2.dll", str5 + "/libGLESv2.dll");
            }
        }
    }

    public static void PostProcessUniversalLinuxPlayer(BuildTarget target, BuildOptions options, string installPath, string stagingAreaData, string stagingArea, string playerPackage)
    {
        installPath = Path.Combine(Path.GetDirectoryName(installPath), Path.GetFileNameWithoutExtension(installPath));
        string playbackEngineDirectory = BuildPipeline.GetPlaybackEngineDirectory(BuildTarget.StandaloneLinux, options);
        string str2 = BuildPipeline.GetPlaybackEngineDirectory(BuildTarget.StandaloneLinux64, options);
        FileUtil.CopyFileOrDirectory(Path.Combine(playbackEngineDirectory, "LinuxPlayer"), Path.Combine(stagingArea, "LinuxPlayer." + GetArchitectureForTarget(BuildTarget.StandaloneLinux)));
        FileUtil.CopyFileOrDirectory(Path.Combine(str2, "LinuxPlayer"), Path.Combine(stagingArea, "LinuxPlayer." + GetArchitectureForTarget(BuildTarget.StandaloneLinux64)));
        string dir = Path.Combine(stagingArea, "Data/Resources");
        FileUtil.CreateOrCleanDirectory(dir);
        FileUtil.CopyDirectoryRecursive(Path.Combine(playbackEngineDirectory, "Data/Resources"), dir);
        string str4 = Path.Combine(stagingArea, Path.Combine("Data/Mono", GetArchitectureForTarget(BuildTarget.StandaloneLinux)));
        string str5 = Path.Combine(stagingArea, Path.Combine("Data/Mono", GetArchitectureForTarget(BuildTarget.StandaloneLinux64)));
        FileUtil.CreateOrCleanDirectory(str4);
        FileUtil.CreateOrCleanDirectory(str5);
        FileUtil.CopyFileOrDirectory(Path.Combine(playbackEngineDirectory, "Data/Mono/libmono.so"), Path.Combine(str4, "libmono.so"));
        FileUtil.CopyFileOrDirectory(Path.Combine(str2, "Data/Mono/libmono.so"), Path.Combine(str5, "libmono.so"));
        PostprocessBuildPlayer.InstallPlugins(stagingArea + "/Data/Plugins", target);
        string str6 = FileUtil.UnityGetFileNameWithoutExtension(installPath);
        string str7 = FileUtil.UnityGetDirectoryName(installPath);
        string str8 = str6 + "_Data";
        string path = Path.Combine(str7, str8);
        FileUtil.DeleteFileOrDirectory(installPath + "." + GetArchitectureForTarget(BuildTarget.StandaloneLinux));
        FileUtil.DeleteFileOrDirectory(installPath + "." + GetArchitectureForTarget(BuildTarget.StandaloneLinux64));
        FileUtil.DeleteFileOrDirectory(path);
        FileUtil.MoveFileOrDirectory(stagingArea + "/Data", path);
        FileUtil.MoveFileOrDirectory(stagingArea + "/LinuxPlayer." + GetArchitectureForTarget(BuildTarget.StandaloneLinux), installPath + "." + GetArchitectureForTarget(BuildTarget.StandaloneLinux));
        FileUtil.MoveFileOrDirectory(stagingArea + "/LinuxPlayer." + GetArchitectureForTarget(BuildTarget.StandaloneLinux64), installPath + "." + GetArchitectureForTarget(BuildTarget.StandaloneLinux64));
    }

    private static void RemoveArch(string path, BuildTarget target)
    {
        Unsupported.StripFatMacho(path, false);
    }

    public static string subDir32Bit
    {
        get
        {
            return "x86";
        }
    }

    public static string subDir64Bit
    {
        get
        {
            return "x86_64";
        }
    }
}

